package com.itboye.hutouben.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.mysetting.SettingActivity;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.bean.PersonDataBean;
import com.itboye.hutouben.logincontroller.LoginController;
import com.itboye.hutouben.logincontroller.LoginedState;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册3
 */
public class RegisterThreeActivity extends BaseActivity implements Observer {
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    EditText res_three_num,res_three_yqm;
    ToggleButton res_three_tb;
    TextView res_three_ok;

    String code;
    String phone;

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_three_activity);
        add_shap_title_tv.setText(R.string.register);

        userPresenter=new UserPresenter(this);

        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;
            case R.id.res_three_ok:
                if (TextUtils.isEmpty(res_three_num.getText().toString())) {
                    ByAlert.alert("请输入密码");
                    return;
                }
                showProgressDialog("请稍后...", true);
                userPresenter.register(phone,res_three_num.getText().toString(),"+86",code,0+"",res_three_yqm.getText().toString());
                break;
            case R.id.res_three_tb:// 密码可见
                if (res_three_tb.isChecked()) {
                    res_three_num
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    res_three_num.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }
                break;
            default:
                break;
        }
    }
    /**
     * 登录
     */
    public void login() {
        userPresenter = new UserPresenter(this);
        userPresenter.login(phone, res_three_num.getText().toString());
    }
    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.register_success){
                ByAlert.alert(entity.getData());

                login();

            }
            if(entity.getEventType()==UserPresenter.register_fail){
                ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.login_success){
                PersonDataBean bean = (PersonDataBean) entity.getData();

                if (bean != null) {
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.ID, bean.getId());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.PaySecret, bean.getPaySecret());
                    Log.d("PaySecret",bean.getPaySecret());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.USERNAME, bean.getUsername());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.PASSWORD, bean.getPassword());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.MOBILE, bean.getMobile());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.IS_LOGINED, true);
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.HEAD, bean.getHead());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.NICK, bean.getNickname());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.YQM, bean.getIdcode());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.ISAUTH, bean.getRolesInfo().get(0).getIsAuth());
                    SPUtils.put(RegisterThreeActivity.this, null,
                            Const.S_ID, bean.getAutoLoginCode());

                    LoginController.setLoginState(new LoginedState());

                    // 发送广播通知LoginActivity结束
                    Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
                    sendBroadcast(intent1);
                    Intent intent2 = new Intent(SettingActivity.EXITCHANGE);// 通知MeFragment更新界面
                    sendBroadcast(intent2);
                    RegisterThreeActivity.this.finish();

                }

            }else if(entity.getEventType()==UserPresenter.login_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
